package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author lupengning
 * @since 2021-05-27
 */

/**
 * @author lupengning
 * @version 1.0
 * @description:
 * @date 2021/6/15 19:33
 */
@Slf4j
@RestController
@Api(tags = "借款人信息")
@RequestMapping("/api/core/borrowerInfo")
public class ApiBorrowInfoController {

  @Resource
  private BorrowInfoService borrowInfoService;

  /**
   * 获取借款额度
   * @return
   */
  @ApiOperation("获取借款额度")
  @GetMapping("/auth/getBorrowAmount")
  public R getBorrowAmount(HttpServletRequest request) {
    String token = request.getHeader("token");
    Long userId = JwtUtils.getUserId(token);
    BigDecimal borrowAmount = borrowInfoService.getBorrowAmount(userId);
    return R.ok().data("borrowAmount", borrowAmount);

  }

  /**
   * 提交借款申请
   * @return
   */
  @ApiOperation("提交借款申请")
  @PostMapping("/auth/save")
  public R save(@RequestBody BorrowInfo borrowInfo,HttpServletRequest request) {

    String token = request.getHeader("token");
    Long userId = JwtUtils.getUserId(token);
    borrowInfoService.saveBorrowInfo(borrowInfo, userId);
    return R.ok().message("提交成功");

  }

  @ApiOperation("获取借款申请审批状态")
  @GetMapping("/auth/getBorrowInfoStatus")
  public R getBorrowerStatus(HttpServletRequest request){
    String token = request.getHeader("token");
    Long userId = JwtUtils.getUserId(token);
    Integer status = borrowInfoService.getStatusByUserId(userId);
    return R.ok().data("borrowInfoStatus", status);
  }

}

